from http import HTTPStatus
from dashscope import VideoSynthesis
import dashscope
from multi_modal.public import get_tongyi_api
from loguru import logger


def generate_video(text) -> str | None:
    """
    文字生成视频
    :param text: 文字内容，用于生成视频
    :return:    生成的视频URL    失败返回None
    """
    dashscope.api_key = get_tongyi_api()
    logger.debug('开始视频合成...')
    rsp = VideoSynthesis.call(model='wanx2.1-t2v-turbo',
                              prompt=text,
                              size='720*1280')

    if rsp.status_code == HTTPStatus.OK:
        logger.debug('视频合成成功!')
        return rsp.output.video_url
    else:
        logger.debug('Failed, status_code: %s, code: %s, message: %s' % (rsp.status_code, rsp.code, rsp.message))
        return None


if __name__ == '__main__':
    print(generate_video('一只橘猫在和金毛嬉闹'))
